<!-- 批量入库 -->
<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from 'vue'
import {
  NSpace,
  NButton,
  type FormInst,
  NForm,
  NFormItem,
  type FormRules,
  useMessage,
  NScrollbar,
  NSpin,
  NInput,
  NSelect,
  type DataTableColumns,
  NPagination,
  NDataTable
} from 'naive-ui'
import { sendRequest } from '@/utils/api'
import FixedAction from '@/components/FixedAction.vue'
import TableView from '@/components/TableView.vue'
import { useThingStore } from '@/stores/addThing'
import { useRoute } from 'vue-router'
import useRouteReplace from '@/hooks/useRouteReplace'
const route = useRoute()
const routeReplace = useRouteReplace(route.query.redirect as string)
const tableData: any = ref()
const message = useMessage()
const userFormRef = ref<FormInst | null>(null)
const loading = ref(false)
const form: any = reactive({
  id: null,
  key: null,
  collectTypeCode: null,
  indexName: null
})
const rowKey: any = (e: any) => e.subItemCode || ''
const thingStore = useThingStore()
//筛选条件
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    queryTableData(true)
  }
})
const columns: DataTableColumns = [
  {
    type: 'selection',
    width: '1rem'
  },
  {
    title: '序号',
    key: 'index',
    width: '1rem',
    align: 'center',
    render: (_, i) => {
      return ++i + (paginationReactive.page - 1) * paginationReactive.pageSize
    }
  },
  {
    title: '版本名称',
    key: 'itemName',
    width: '3rem',
    align: 'center'
  },
  {
    title: '馆藏码',
    key: 'assetCode',
    width: '3rem',
    align: 'center'
  },
  {
    title: '征集方式',
    key: 'collectTypeName',
    width: '3rem',
    align: 'center'
  },
  // {
  //   title: '版本编号',
  //   key: 'subItemCode',
  //   width: '5rem',
  //   align: 'center'
  // },
  {
    title: '类型',
    key: 'itemTypeName',
    width: '3rem',
    align: 'center'
  },
  {
    title: '库房位置',
    key: 'positionName',
    width: '3rem',
    align: 'center'
  }
  // {
  //   title: '入库位置',
  //   key: 'collectTypeName',
  //   width: '10rem',
  //   align: 'center',
  //   render: (e: any) => {
  //     return (
  //       (e.firstLevelName || '') +
  //       ' ' +
  //       (e.secondLevelName || '') +
  //       ' ' +
  //       (e.threeLevelName || '') +
  //       ' ' +
  //       (e.fourthLevelName || '') +
  //       ' ' +
  //       (e.fifthLevelName || '') +
  //       ' ' +
  //       (e.sixthLevelName || '')
  //     )
  //   }
  // }
]

const formRules: FormRules = {
  file: {
    required: true,
    trigger: ['blur', 'change'],
    type: 'any',
    message: '不能为空'
  }
}
const tableLoading = ref()
const collectFactionList: any = ref()
const queryTableData = async (resetPage = false) => {
  if (resetPage) paginationReactive.page = 1
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'collectProcess',
    api: 'listCollecting',
    override: {
      params: {
        collectTypeCode: form.collectTypeCode,
        key: form.key,
        limit: paginationReactive.pageSize,
        page: paginationReactive.page
      }
    }
  })
  loading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  tableData.value = res.data.list
  paginationReactive.itemCount = res.data.totalCount
  tableLoading.value = false
}
// 字典下拉
const codeFn = async (code: any, refValue: any) => {
  const [error, res] = await sendRequest({
    module: 'collectProcess',
    api: 'listByParentCode',
    override: {
      params: {
        dicCode: code
      }
    }
  })
  if (error) return message.error(error.message)
  refValue.value = res?.data?.map((it: any) => {
    return { value: it.dicCode, label: it.dicName }
  })
}
//多选框勾选
type DataListSelectionsItem = {
  id: string
}
const notIssuedId = ref()
const itemSelectList = ref()
const dataListSelections: any = ref<DataListSelectionsItem[]>([])
function selectionChangeHandle(val: any, info: any) {
  dataListSelections.value = val
  itemSelectList.value = info
  notIssuedId.value = []
  info.map((i: any) => {
    if (i.distributionStatus == '0') {
      notIssuedId.value.push(i.id)
    }
  })
}

const submit = async () => {
  thingStore.thingList = itemSelectList.value

  goBack()
}
const reset = () => {
  form.key = null
  form.collectTypeCode = null
}
// 返回
const goBack = () => {
  // history.back()
  routeReplace()
}
onMounted(async () => {
  codeFn('A30', collectFactionList) //征集方式A30
  queryTableData()
})
watch(
  () => route.path,
  () => {
    if (route.path == '/collection/process/pillManage/addInfoList') {
      itemSelectList.value = []
      dataListSelections.value = []
    }
  },
  { deep: true, immediate: true }
)
</script>

<template>
  <div class="main">
    <div class="form_content">
      <NScrollbar>
        <NSpin :show="false">
          <div class="content">
            <!-- <NForm
              :model="form"
              :rules="formRules"
              ref="userFormRef"
              label-placement="left"
              label-width="110"
            ></NForm> -->
            <TableView class="table">
              <template #form>
                <NSpace justify="space-between">
                  <NForm
                    :show-feedback="false"
                    label-placement="left"
                    inline
                  >
                    <NSpace>
                      <NFormItem label="藏品名称:">
                        <NInput
                          placeholder="请输入"
                          v-model:value="form.key"
                        />
                      </NFormItem>
                      <NFormItem label="征集方式:">
                        <NSelect
                          :options="collectFactionList"
                          v-model:value="form.collectTypeCode"
                          placeholder="请选择征集方式"
                          clearable
                          filterable
                          style="width: 200px; margin-right: 10rem"
                        />
                      </NFormItem>
                    </NSpace>
                    <NFormItem>
                      <NButton
                        type="primary"
                        style="width: 78px"
                        ghost
                        @click="queryTableData(true)"
                      >
                        查询
                      </NButton>
                    </NFormItem>
                    <NFormItem>
                      <NButton
                        style="width: 78px; margin-right: 40rem"
                        ghost
                        type="primary"
                        @click="reset()"
                      >
                        重置
                      </NButton>
                    </NFormItem>
                  </NForm>
                </NSpace>
              </template>
              <template #table="props">
                <NDataTable
                  v-bind="props"
                  :bordered="false"
                  :row-key="rowKey"
                  :columns="columns"
                  :data="tableData"
                  :loading="tableLoading"
                  :checked-row-keys="dataListSelections"
                  @update:checked-row-keys="selectionChangeHandle"
                />
              </template>
              <template #pagination>
                <NPagination
                  show-size-picker
                  show-quick-jumper
                  :page-sizes="paginationReactive.pageSizes"
                  :item-count="paginationReactive.itemCount"
                  :page="paginationReactive.page"
                  :page-size="paginationReactive.pageSize"
                  @update:page="paginationReactive.onChange"
                  @update:page-size="paginationReactive.onUpdatePageSize"
                  :suffix="() => '页'"
                >
                  <template #prefix>
                    <span style="font-size: 14px">共{{ paginationReactive.itemCount }}条</span>
                  </template>
                </NPagination>
              </template>
            </TableView>
          </div>
        </NSpin>
      </NScrollbar>
    </div>
  </div>
  <FixedAction>
    <NSpace justify="center">
      <NButton
        style="width: 78px"
        ghost
        @click="goBack"
      >
        取消
      </NButton>
      <NButton
        type="primary"
        style="width: 78px"
        @click="submit"
        :loading="loading"
      >
        确定
      </NButton>
    </NSpace>
  </FixedAction>
</template>
<style scoped lang="scss">
@mixin CommonFont {
  font-size: 18rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #333333;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .form_content {
    width: 100%;
    height: 100%;
    padding: 20rem 20rem 0;

    .topTitle {
      font-size: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      color: #5fb17f;
      margin-bottom: 10rem;
    }

    .inputWH {
      width: 300rem;
    }

    .inputWHmin {
      width: 150rem;
    }

    .content {
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 18rem 33rem 120rem;
      overflow-y: scroll;
      background-color: #ebeceb;
      @include CommonFont;
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
